﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MoneyLover.WCFMoneyLover;

namespace MoneyLover.Pages
{
    public partial class Chi : PhoneApplicationPage
    {
        public Chi()
        {
            InitializeComponent();
        }
        MoneyLoverServiceClient _db = new MoneyLoverServiceClient();
        THU_CHI thuchi = new THU_CHI();
        private void txtkehoach_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lstkehoach.Visibility == Visibility.Collapsed)
            {
                lstkehoach.Visibility = Visibility.Visible;
            }
            else
            {
                lstkehoach.Visibility = Visibility.Collapsed;
            }
           

        }

        private void txttheloai_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lstlistoaichi.Visibility == Visibility.Collapsed)
            {
                lstlistoaichi.Visibility = Visibility.Visible;
            }
            else
            {
                lstlistoaichi.Visibility = Visibility.Collapsed;
            }
        }

        private void lstlistoaichi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstlistoaichi.Visibility = Visibility.Collapsed;
            var objdb = (LOAIGIAODICH)lstlistoaichi.SelectedItem;
            txttheloai.Text = objdb.TENLOAIGD;
            thuchi.MALOAIGD = objdb.ID;
        }

        private void lstkehoach_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstkehoach.Visibility = Visibility.Collapsed;
            var objdb = (KEHOACH)lstkehoach.SelectedItem;
            txtkehoach.Text = objdb.TENKH;
            thuchi.MAKH = objdb.ID;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            loadreference();
        }

        private void loadreference()
        {
            _db.GetListLoaiChiCompleted += _db_GetListLoaiChiCompleted;
            _db.GetListLoaiChiAsync();
            _db.GetListKeHoachCompleted += _db_GetListKeHoachCompleted;
            _db.GetListKeHoachAsync();
            lstlistoaichi.Visibility = Visibility.Collapsed;
            lstkehoach.Visibility = Visibility.Collapsed;
        }

        void _db_GetListKeHoachCompleted(object sender, GetListKeHoachCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lstkehoach.ItemsSource = e.Result;

            }
        }

        void _db_GetListLoaiChiCompleted(object sender, GetListLoaiChiCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lstlistoaichi.ItemsSource = e.Result;

            }
        }

        private void btnsave_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(Invalid()))
            {
                thuchi.TENGD = txttenkhoanchi.Text.Trim();
                thuchi.SOTIEN = decimal.Parse(txtsotien.Text.Trim());
                thuchi.GHICHU = txtghichu.Text.Trim();
                //thuchi.NGAYGD = DateTime.Now;
                thuchi.LAPLAI = false;
                String machi = NavigationContext.QueryString["id"];
                if (!String.IsNullOrEmpty(machi))
                {
                    int magd = int.Parse(machi);
                    thuchi.MAGD = magd;
                }
                thuchi.USERNAME = Contanst._tkdangnhap.USERNAME;
                _db.InsertThuChiCompleted += _db_InsertThuChiCompleted;
                _db.InsertThuChiAsync(thuchi);
            }
            else
            {
                MessageBox.Show(Invalid());
            }
        }

        void _db_InsertThuChiCompleted(object sender, InsertThuChiCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                MessageBox.Show("Thêm khoản chi không thành công");
            }
            else
            {
                NavigationService.Navigate(new Uri("/Pages/SoChiTieu.xaml", UriKind.Relative));
            }
        }
        void _db_InsertThuChiCompletedAdd(object sender, InsertThuChiCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                MessageBox.Show("Thêm khoản chi không thành công");
            }
            else
            {
                NavigationService.Navigate(new Uri("/Pages/Giaodich.xaml", UriKind.Relative));
            }
        }
        private void bntsaveandadd_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(Invalid()))
            {
                thuchi.TENGD = txttenkhoanchi.Text.Trim();
                thuchi.SOTIEN = decimal.Parse(txtsotien.Text.Trim());
                thuchi.GHICHU = txtghichu.Text.Trim();
                //thuchi.NGAYGD = DateTime.Now;
                thuchi.LAPLAI = false;
                String machi = NavigationContext.QueryString["id"];
                if (!String.IsNullOrEmpty(machi))
                {
                    int magd = int.Parse(machi);
                    thuchi.MAGD = magd;
                }
                thuchi.USERNAME = Contanst._tkdangnhap.USERNAME;
                _db.InsertThuChiCompleted += _db_InsertThuChiCompletedAdd;
                _db.InsertThuChiAsync(thuchi);
            }
            else
            {
                MessageBox.Show(Invalid());
            }
        }
        private string Invalid()
        {
            String error = "";
            if (String.IsNullOrEmpty(txttenkhoanchi.Text.Trim()))
            {
                error += "\n Tên khoản thu không được để trống";
            }
            try
            {
                float a = float.Parse(txtsotien.Text.Trim());
            }
            catch
            {
                error += "\n số tiền không hợp lệ";
            }
            if (String.IsNullOrEmpty(txtngaychi.Text.Trim()))
            {
                error += "\n Chưa nhập ngày tháng";
            }
            else
            {
                string words = txtngaychi.Text.Trim();
                try
                {
                    string[] split = words.Split(new Char[] { '/' });
                    int ngay = int.Parse(split[0]);
                    int Thang = int.Parse(split[1]);
                    int nam = int.Parse(split[2]);
                    thuchi.NGAYGD = new DateTime(nam, Thang,ngay);
                }
                catch 
                {
                	error+="\nNgày chi không hợp lệ";
                }
                

            }
            return error;

        }

        private void ngaychi_MouseEnter(object sender, MouseEventArgs e)
        {
            txtngaychi.Text = "";
        }

        private void txtngaychi_MouseLeave(object sender, MouseEventArgs e)
        {
            if (String.IsNullOrEmpty(txtngaychi.Text.Trim()))
            {
                txtngaychi.Text = "dd/mm/yyyy";
            }
        }
    }
}